<template>
	<f-popup ref="popup" position="bottom">
		<view class="refund-order">
			<view class="head-title">确认订单</view>
			<scroll-view scroll-y class="scroll" style="height: 54vh;">
				<!-- <view class="address" @click="goAddress">
					<view v-if="!address.linkman" class="icon-btn">
						<view class="iconfont icon-add"></view>
						<view class="">添加收货地址</view>
					</view>
					
					<view v-else class="list-item">
						<view class="padding-r24">
							<view class="area">{{address.province}}{{address.city}}{{address.area}}</view>
							<view class="detail">{{address.address}}</view>
							<view class="flex align-center" style="gap: 16rpx;">
								<view class="name">{{address.linkman}}</view>
								<view class="phone">{{address.phone}}</view>
							</view>
						</view>
						<view class="iconfont icon-chevron-left"></view>
					</view>
				</view> -->
				
				<view class="card">
					<view class="form">
						<view class="form-item">
							<view class="form-label">退款金额<text style="color: red;">*</text></view>
							<view class="form-body">
								<view class="input">
									<input type="digit" v-model="params.refund_amount" placeholder="请输入退款金额" placeholder-class="placeholder-class" />
								</view>
							</view>
						</view>
						<view class="form-item">
							<view class="form-label">退款描述<text style="color: red;">*</text></view>
							<view class="form-body" style="border-bottom: none;">
								<view class="textarea">
									<textarea v-model="params.refund_msg" placeholder="请输入退款描述" placeholder-class="placeholder-class"></textarea>
								</view>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
			
			<view class="footer-btns">
				<button class="confirm-btn flex-1" @click="handleConfirm">确认</button>
			</view>
		</view>
	</f-popup>
	
</template>

<script>
	const checker = require("@/utils/checker.js");
	
	export default {
		name: 'refund-order',
		data() {
			return {
				params: {
					refund_amount: '', // 退款金额
					refund_msg: '', // 退款描述
				},
				rule: [
					{ name: "refund_amount",checkType: "notnull",checkRule: "",errorMsg: "请输入退款金额" },
					{ name: "refund_msg",checkType: "notnull",checkRule: "",errorMsg: "请输入退款描述" },
				],
				address: {},
				info: {}
			};
		},
		computed: {
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			}
		},
		onReady() {
			uni.$on('changeAddress', (val)=> {
				this.params.address_gid = val.gid
				this.address = val
			})
		},
		methods: {
			handleConfirm() {
				const formData = { ...this.params }
				const checkRes = checker.check(formData, this.rule);
				if (checkRes) {
					this.handleSubmit(formData)
				} else {
					uni.showToast({
						title: checker.error,
						icon: "none"
					});
				}
			},
			handleSubmit() {
				this.$api.agentUserRentOrderRefundApi(this.info.gid, this.params).then(res=> {
					uni.showToast({
						title: '退款成功'
					})
					this.$emit('refresh')
					this.close()
				}).catch(err=> {
					console.log('err', err);
				})
			},
			// 前往地址
			goAddress() {
				uni.navigateTo({
					url: '/pages/address/address?type=select'
				})
			},
			open(info) {
				this.info = info
				this.params = {
					refund_amount: this.info.pay_total_fee, // 退款金额
					refund_msg: '', // 退款描述
				}
				this.$refs.popup.open()
			},
			close() {
				this.$refs.popup.close()
			},
			formatDate(date) {
				// 原始日期字符串
				const dateStr = '2024年09月26日';
				// 使用正则表达式提取年、月、日
				const dateParts = date.match(/(\d{4})年(\d{2})月(\d{2})日/);
				if (dateParts) {
				  // 组装为 'YYYY-MM-DD' 格式
				  const formattedDate = `${dateParts[1]}-${dateParts[2]}-${dateParts[3]}`;
				  return formattedDate
				}
				return ''
			},
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();

				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			}
		}
	}
</script>

<style lang="scss" scoped>
	.refund-order {
		width: 100%;
		padding: 0 32rpx;
		box-sizing: border-box;
		background-color: #F6F6F6;
		border-top-left-radius: 26rpx;
		border-top-right-radius: 26rpx;
		
		.head-title {
			font-size: 34rpx;
			font-weight: 600;
			line-height: 46rpx;
			text-align: center;
			color: #000000;
			padding: 32rpx;
		}
		
		.scroll {
			padding-bottom: calc(120rpx+ constant(safe-area-inset-bottom)); ///兼容 IOS<11.2/
			padding-bottom: calc(120rpx + env(safe-area-inset-bottom)); ///兼容 IOS>11.2/
		}
		
		.address {
			min-height: 214rpx;
			padding: 40rpx;
			margin-bottom: 32rpx;
			border-radius: 26rpx;
			box-sizing: border-box;
			background-color: #FFFFFF;
			
			.icon-btn {
				display: flex;
				align-items: center;
				justify-content: center;
				gap: 28rpx;
				height: 134rpx;
				
				font-size: 34rpx;
				font-weight: 400;
				line-height: 46rpx;
				text-align: left;
				color: #000000;
				
				.icon-add {
					font-size: 30rpx;
				}
			}
			
			.list-item {
				position: relative;
				overflow: hidden;
				background-color: #FFFFFF;
				box-shadow: 0px 0px 33rpx rgba(0, 0, 0, 0.02);
				
				display: flex;
				align-items: center;
				justify-content: space-between;
			}
			
			.label {
				position: absolute;
				top: 0;
				right: 0;
				z-index: 9;
				border-radius: 6rpx;
				padding: 4rpx 16rpx;
				background-color: #E3F9E9;
				
				font-size: 24rpx;
				font-weight: 400;
				line-height: 40rpx;
				text-align: center;
				color: #2BA471;
			}
			
			.area {
				font-family: PingFang SC;
				font-size: 13px;
				font-weight: 400;
				line-height: 18.2px;
				text-align: left;
				color: rgba(0, 0, 0, 0.7);
				padding-bottom: 14rpx;
			}
			
			.detail {
				font-size: 34rpx;
				font-weight: 400;
				line-height: 46rpx;
				text-align: left;
				color: #000000;
				padding-bottom: 14rpx;
			}
			
			.name {
				font-size: 26rpx;
				font-weight: 400;
				line-height: 36rpx;
				text-align: left;
				color: #000000;
			}
			
			.phone {
				font-size: 26rpx;
				font-weight: 400;
				line-height: 36rpx;
				text-align: left;
				color: #000000;
			}
			
			.icon-edit-1 {
				color: #000000;
				font-size: 32rpx;
				position: relative;
				z-index: 9;
				
				&::after {
					position: absolute;
					content: '';
					top: 14rpx;
					left: 4rpx;
					z-index: -1;
					width: 70%;
					height: 8rpx;
					transform: rotate(-45deg);
					background-color: #A5A4B4;
				}
			}
		}
		
		.card {
			padding: 38rpx 32rpx;
			border-radius: 16rpx;
			margin-bottom: 32rpx;
			box-sizing: border-box;
			background-color: #FFFFFF;
			box-shadow: 0px 0px 33rpx rgba(0, 0, 0, 0.05);
		}
		
		.form {
		
			.form-item {
				padding-bottom: 32rpx;
			
				&:last-child {
					padding-bottom: 0;
				}
			}
			
			.form-label {
				font-size: 28rpx;
				font-weight: 400;
				text-align: left;
				line-height: 44rpx;
				padding-bottom: 8rpx;
				color: rgba(0, 0, 0, 0.9);
			}
			
			.form-body {
				padding-bottom: 12rpx;
				border-bottom: 1px solid #E7E7E7;
			}
			
			.input {
				width: 100%;
				height: 72rpx;
				display: flex;
				align-items: center;
			
				input {
					flex: 1;
					height: 100%;
				}
			}
			
			.picker {
				width: 100%;
				height: 72rpx;
				display: flex;
				align-items: center;
				
				picker {
					width: 100%;
					height: 72rpx;
				}
				.picker-body {
					width: 100%;
					height: 72rpx;
					display: flex;
					align-items: center;
					box-sizing: border-box;
				}
			}
			
			.hint {
				font-size: 24rpx;
				font-weight: 400;
				text-align: left;
				line-height: 40rpx;
				padding-bottom: 10rpx;
				color: rgba(0, 0, 0, 0.4);
			}
			
			.right-btn {
				position: relative;
				padding: 0 32rpx;
				height: 48rpx;
				color: var(--primary-color);
				font-size: 32rpx;
				font-weight: 400;
				line-height: 48rpx;
				text-align: center;
				line-height: 48rpx;
			
				&:before {
					position: absolute;
					content: '';
					top: 5%;
					left: 0;
					bottom: 0;
					width: 1px;
					height: 90%;
					background-color: #E7E7E7;
				}
			}
			
			.textarea {
				height: 168rpx;
				padding: 14rpx 16rpx;
				border-radius: 12rpx;
				box-sizing: border-box;
				border: 1px solid #DCDCDC;
			
				textarea {
					width: 100%;
					height: 100%;
					font-size: 32rpx;
					font-weight: 400;
					line-height: 48rpx;
					text-align: left;
				}
			}
			
		}
	
		
		
		.footer-btns {
			position: fixed;
			left: 0;
			right: 0;
			bottom: 0;
			width: 100%;
			z-index: 9999;
			padding: 20rpx 32rpx;
			box-sizing: border-box;
			background-color: #FFFFFF;
			box-shadow: 0px 0px 33rpx rgba(0, 0, 0, 0.05);
			padding-bottom: calc(20rpx + constant(safe-area-inset-bottom)); ///兼容 IOS<11.2/
			padding-bottom: calc(20rpx + env(safe-area-inset-bottom)); ///兼容 IOS>11.2/
		
			gap: 14rpx;
			display: flex;
			align-items: center;
		}
	}
</style>
